package com.nkl.page.dao;

import com.nkl.common.dao.BaseDao;
import com.nkl.common.util.StringUtil;
import com.nkl.page.domain.Comment;
import com.nkl.page.domain.User;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

//import com.nkl.common.util.DateUtil;
//import com.nkl.page.domain.Infos;

public class CommentDao {

	public int addComment(Comment comment, Connection conn){
        String sql = "INSERT INTO comment(comment_id,user_id,comment_title,comment_content,comment_date,comment_click,comment_pic,upload_pic) values(null,?,?,?,?,?,?,?)";
        Object[] params = new Object[]{
                comment.getUser_id(),
                comment.getComment_title(),
                comment.getComment_content(),
                comment.getComment_date(),
                comment.getComment_click(),
                comment.getComment_pic(),
                comment.getUpload_pic()
        };
		return BaseDao.executeUpdate(sql, params, conn );
	}

	public int delComment(String comment_id, Connection conn){
		String sql = "DELETE FROM comment WHERE comment_id=?";

		Object[] params = new Object[] { new Integer(comment_id)};
		return BaseDao.executeUpdate(sql, params, conn);
	}

	public int delComments(String[] comment_ids, Connection conn){
		StringBuilder sBuilder = new StringBuilder();
		for (int i = 0; i <comment_ids.length; i++) {
			sBuilder.append("?");
			if (i !=comment_ids.length-1) {
				sBuilder.append(",");
			}
		}
		String sql = "DELETE FROM comment WHERE comment_id IN(" +sBuilder.toString()+")";

		Object[] params = comment_ids;

		return BaseDao.executeUpdate(sql, params, conn);	}

	public int updateComment(Comment comment, Connection conn){
		StringBuilder sBuilder = new StringBuilder();
		sBuilder.append("UPDATE comment SET comment_id = " + comment.getComment_id());

		sBuilder.append(" where comment_id = " + comment.getComment_id());

		Object[] params = null;
		return BaseDao.executeUpdate(sBuilder.toString(), params, conn);	}

	public Comment getComment(Comment comment, Connection conn){
		Comment _comment =null;
		StringBuilder sBuilder = new StringBuilder();
		sBuilder.append("SELECT m.*,u.nick_name,u.user_no FROM comment m join user u on m.user_id = u.user_id WHERE comment_id = " + comment.getComment_id());

		List<Object> list = BaseDao.executeQuery(Comment.class.getName(), sBuilder.toString(), null, conn);
		if (list != null && list.size() > 0) {
			 _comment = (Comment)list.get(0);
		}
		return _comment;
	}

	public List<Comment>  listComments(Comment comment, Connection conn){
		List<Comment> comments = null;
		StringBuilder sBuilder = new StringBuilder();
		sBuilder.append("SELECT * FROM (");
		sBuilder.append("SELECT m.*,u.nick_name,u.user_no FROM comment m join user u on m.user_id = u.user_id WHERE 1=1");
		if (comment.getComment_id()!=0) {
			sBuilder.append(" and comment_id = " + comment.getComment_id());
		}
		if (!StringUtil.isEmptyString(comment.getComment_title())) {
			sBuilder.append(" and comment_title like '%" + comment.getComment_title() + "%'");
		}

		sBuilder.append(" order by comment_date desc,comment_id asc) t");

		if (comment.getStart() != -1) {
			sBuilder.append(" limit " + comment.getStart() + "," + comment.getLimit());
		}

		List<Object> list = BaseDao.executeQuery(Comment.class.getName(), sBuilder.toString(), null, conn);
		if (list != null && list.size() > 0) {
			comments = new ArrayList<Comment>();
			for (Object object : list) {
				comments.add((Comment)object);
			}
		}
		return comments;
	}

	public int listCommentsCount(Comment comment, Connection conn){
		int sum = 0;
		StringBuilder sBuilder = new StringBuilder();
		sBuilder.append("SELECT count(*) FROM comment WHERE 1=1");

		if (comment.getComment_id()!=0) {
			sBuilder.append(" and comment_id = " + comment.getComment_id());
		}
		if (!StringUtil.isEmptyString(comment.getComment_title())) {
			sBuilder.append(" and comment_title like '%" + comment.getComment_title() + "%'");
		}

		long count = (Long)BaseDao.executeQueryObject(sBuilder.toString(), null, conn);
		sum = (int)count;

		return sum;
	}

	/**
	 * @Title: getCommentId
	 * @Description: 得到当前用户最新信息ID
	 * @param user
	 * @param conn
	 * @return int
	 */
	public int getCommentId(User user,Connection conn){
		int comment = 0;
		StringBuilder sBuilder = new StringBuilder();
		sBuilder.append("SELECT max(comment_id) FROM comment WHERE user_id="+user.getUser_id());

		comment = (Integer)BaseDao.executeQueryObject(sBuilder.toString(), null, conn);

		return comment;
	}

}
